﻿app.controller('ReserveCtrl', ['$scope', '$http', 'httpPlus', '$state','$filter', function ($scope, $http, httpPlus, $state,$filter) {
    var currUrl = location.href.split('#')[0];
    var longitude = '';
    var latitude = '';
    var geolocation = new qq.maps.Geolocation("OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77", "myapp");
    $scope.data = {
        searchValue: '',
        addressList: [],
        cityName:'',
        reserveTime: undefined,
        selectAddress: undefined,
        minTime:new Date(new Date().setMinutes(new Date().getMinutes()+15)),
    };
    geolocation.getIpLocation(showPosition,showErr);
    function showPosition(position) {
        $scope.data.cityName = position.city;
        longitude = position.lng;
        latitude = position.lat;
    };
    function showErr(err) {
        alert(JSON.stringify(err));
    };

    $scope.event = {
        clearValue_click: function () {
            $scope.data.searchValue = '';
        },
        search_click: function () {
            longitude = ''; // 经度，浮点数，范围为180 ~ -180。
            latitude = '';
            getAddressList(0);
        },
        showMore_click: function () {
            longitude = ''; // 经度，浮点数，范围为180 ~ -180。
            latitude = '';
            getAddressList(1);
        },
        goToOrder_click: function () {
            if ($scope.data.reserveTime == undefined || $scope.data.reserveTime == '') {
                return alert('预定时间不能为空!');
            }
            else if ($scope.data.selectAddress == undefined) {
                return alert('请选择餐厅!');
            } else if($scope.data.reserveTime.getTime()<new Date().setMinutes(new Date().getMinutes()+15)){
                return alert('预定时间必须为15分钟后的时间');
            }else { $state.go('wx.reserveOrder',{'reservetime':$scope.data.reserveTime.getTime(),'code':$scope.data.selectAddress.shopCode}) };
        },


    };

    function getAddressList(_more) {
        httpPlus.MMB2RRRApi("MenuBoard/GetShopListForLongitude", {
            _lng: longitude,
            _lat: latitude,
            _serachValue: $scope.data.searchValue,
            _more: _more
        }).then(function (data) {
            $scope.data.addressList = data.shops;

        }).catch(function (err) {
            alert(JSON.stringify(err));
        });
    };
    getAddressList(0);
    //获取微信jssdk配置信息
    /*httpPlus
        .MMB2RRRApi("PyramidSellingBLL/InitWeChatMemberInvitePage", {
            _currUrl: currUrl
        }, 'wechat')
        .then(function (data) {
            // 注入权限验证配置
            wx.config(data.JsConfig);
            wx.ready(function () {
                wx.getLocation({
                    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    success: function (res) {
                        longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                        latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                        geocoder.getAddress(new qq.maps.LatLng(latitude, longitude));
                        getAddressList(0);
                    },
                    cancel: function () {

                    }
                });
            });
            wx.error(function (res) {
                alert(JSON.stringify(res));
            });
        });*/
}]);


